using AutoMapper;
using Domain.Factory_Modeling;
using Infratructure.Interface;
using MediatR;
using ReturnValue;
using System.Collections.Generic;
using System.Linq;

namespace MES.Server.API.Application.Command.Factory_Modeling
{
    public class DelWorkShopHander : IRequestHandler<DelWorkShopCommand, ApiResult<int>>
    {
        private readonly IRepository<WorkShopModel> workShopRepo;
        private readonly IMapper mapper;
        public DelWorkShopHander(IRepository<WorkShopModel> workShopRepo, IMapper mapper)
        {
            this.workShopRepo = workShopRepo;
            this.mapper = mapper;
        }
        /// <summary>
        /// 删除车间信息（支持单删和批删）
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<ApiResult<int>> Handle(DelWorkShopCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();

            // 批删逻辑
            if (request.Ids != null && request.Ids.Any())
            {
                var workShops = new List<WorkShopModel>();
                var notFoundIds = new List<long>();

                foreach (var id in request.Ids)
                {
                    var workShop = await workShopRepo.GetValue(id);
                    if (workShop == null)
                    {
                        notFoundIds.Add(id);
                    }
                    else
                    {
                        workShop.IsDel = true;
                        workShops.Add(workShop);
                    }
                }

                if (workShops.Any())
                {
                    await workShopRepo.UpdateRangeAsync(workShops);
                    result.Code = StatusCode.Success;
                    result.Message = $"成功删除{workShops.Count}个车间信息";
                    result.Data = workShops.Count;

                    if (notFoundIds.Any())
                    {
                        result.Message += $",以下车间不存在: {string.Join(",", notFoundIds)}";
                    }
                }
                else
                {
                    result.Code = StatusCode.Fail;
                    result.Message = "没有找到可删除的车间信息";
                }
            }
            // 单删逻辑
            else
            {
                var workShop = await workShopRepo.GetValue(request.Id);
                if (workShop == null)
                {
                    result.Code = StatusCode.Fail;
                    result.Message = "车间信息不存在,无法删除";
                    return await Task.FromResult(result);
                }
                else
                {
                    workShop.IsDel = true;
                    await workShopRepo.UpdateAsync(workShop);
                    result.Code = StatusCode.Success;
                    result.Message = "删除车间信息成功";
                    result.Data = 1;
                }
            }

            return await Task.FromResult(result);
        }
    }
}
